/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.beans.bind.support;

import java.math.BigDecimal;

import com.bluemartini.dna.Currency;
import com.bluemartini.dna.DNABasePair;
import com.bluemartini.dna.DNACurrency;

/**
 * Provides a currency base pair property editor.
 * 
 * @author Scott Rossillo
 *
 */
public class CurrencyBasePairPropertyEditor implements DNABasePairPropertyEditor {
	
	/**
	 * Creates a new currency base pair property editor.
	 */
	public CurrencyBasePairPropertyEditor() { }
	
	/* (non-Javadoc)
	 * @see net.martinimix.dao.bind.support.DNABasePairPropertyEditor#getAsBasePair(java.lang.Object)
	 */
	public DNABasePair getAsBasePair(Object value) {
		
		return new DNACurrency(new Currency( (BigDecimal) value, "USD" ));
	}
	
	/* (non-Javadoc)
	 * @see net.martinimix.dao.bind.support.DNABasePairPropertyEditor#setAsBasePair(com.bluemartini.dna.DNABasePair)
	 */
	public Object setAsBasePair(DNABasePair value) {
		
		if(DNABasePair.TYPE_CURRENCY != value.getType()) {
			throw new IllegalArgumentException("Given value must be of type currency [" + value + "]");
		}
		
		return ((Currency) value.getValue()).toBigDecimal();
	}
	
}
